package com.lb.jwitter.action;


 import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Collection;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import com.opensymphony.xwork2.Action; 
 import com.lb.jwitter.service.ConditionReport; 
 import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
 import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
 import com.lb.jwitter.service.TblQLDTTXAMonHoc;
 import com.lb.jwitter.service.TblQLDTTXAMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
 import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHocFormAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs; 
    public List<TblQLDTTXALopMonHoc>  getTblQLDTTXALopMonHocs() {
         return tblQLDTTXALopMonHocs; 
      } 

    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService; 

    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 
    
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXAGiaoVien> tblQLDTTXAGiaoViens; 
    public List<TblQLDTTXAGiaoVien>  getTblQLDTTXAGiaoViens(){ 
              return tblQLDTTXAGiaoViens; 
     } 

    private TblQLDTTXAGiaoVienService tblQLDTTXAGiaoVienService; 

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
    { 
        try{ 
      Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "xem")){ 
        return "noprivilege"; 
    }

    tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient"); 
         tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAGiaoVienService = (TblQLDTTXAGiaoVienService) context.getBean("tbl_QLDTTXA_GiaoVienClient"); 


        //get hocky
        Date today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        {
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        	
        	ConditionReport con = new ConditionReport();
            con.setType("date"); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setType("date");
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");            
            cons.add(con2);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
            tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        }

       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        }
        cons.clear();
        
        if(tblQLDTTXAHocKyTrongNam != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1("");
            con.setOperator("max");
            cons.add(con);	
        }
        
        tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocService.findtblQLDTTXALopMonHocByCondition(cons, pageSize, page); 
        pages = tblQLDTTXALopMonHocService.counttblQLDTTXALopMonHocByCondition(cons)/pageSize + 1; 
      
        tblQLDTTXAMonHocs = tblQLDTTXAMonHocService.findAlltblQLDTTXAMonHoc(0, 0);
      
        cons.clear();
        {
        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 20, 1);

      dataString = ""; 
      dataString += "&monHocId=" ; 
      dataString += "&hocKyId=" ; 
      dataString += "&giaoVienId=" ; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_QLDTTXA_LopMonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

	public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
		return tblQLDTTXAHocKyTrongNam;
	}

	public void setTblQLDTTXAHocKyTrongNam(
			TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
		this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
	} 
 } 
 
